package com.zuoban.storage.service.impl;

import com.obs.services.ObsClient;
import com.zuoban.storage.properties.StorageProperties;
import com.zuoban.storage.service.StorageService;

import java.io.InputStream;

/**
 * @BelongsProject: zuoban-boot
 * @BelongsPackage: com.zuoban.storage.service.impl
 * @Author: zxy
 * @CreateTime: 2022-10-10
 * @Description: ...
 */
public class HuaweiStorageServiceImpl implements StorageService {

    private final StorageProperties storageProperties;

    public HuaweiStorageServiceImpl(StorageProperties storageProperties) {
        this.storageProperties = storageProperties;
    }

    @Override
    public String upload(InputStream inputStream, String path) {
        ObsClient client = new ObsClient(storageProperties.getHuawei().getAccessKey(),
                storageProperties.getHuawei().getSecretKey(), storageProperties.getHuawei().getEndPoint());
        try {
            client.putObject(storageProperties.getHuawei().getBucketName(), path, inputStream);
            client.close();
        } catch (Exception e) {
            throw new RuntimeException("上传文件失败：", e);
        }

        return storageProperties.getConfig().getDomain() + "/" + path;
    }
}
